package com.c.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;

import com.c.result.SingleResult;
import com.c.service.exampleService;

import cn.hutool.db.Entity;

@Controller
public class exampleController {

	@Autowired
	private exampleService es;

	@Value("${URL}")
	String URL;

	@RequestMapping("/a")
	@ResponseBody
	public Entity showItem2(Model model, HttpServletRequest request, HttpServletResponse response) {
		List re = (List) es.getList();
		ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
		// 这个参数再SystemInitListener中
		System.out.println(servletContext.getAttribute("user"));
		return (Entity) re.get(0);
	}

	@RequestMapping("/b")
	@ResponseBody
	public String test(HttpServletRequest request, HttpServletResponse response) throws Exception {
		es.testTran();
		return "test";
	}
	
	
	@RequestMapping("/c")
	public String showItem3(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("-------------" + URL);
		List re = (List) es.getList();
		SingleResult<Entity> result = new SingleResult<Entity>();
		result.setData((Entity) re.get(0));
		response.getWriter().write(result.toString());
		return null;
	}
}
